package com.chenyi.framework.web.service;

import com.chenyi.common.config.ChenYiConfig;
import com.chenyi.common.exception.MessageException;
import com.chenyi.common.utils.DateUtils;
import com.chenyi.user.service.ISysUserService;
import com.chenyi.common.constant.Constants;
import com.chenyi.common.core.domain.entity.SysUser;
import com.chenyi.common.core.domain.model.LoginUser;
import com.chenyi.common.enums.UserStatus;
import com.chenyi.common.exception.BadRequestException;
import com.chenyi.common.utils.ApiUtils;
import com.chenyi.common.utils.MessageUtils;
import com.chenyi.common.utils.StringUtils;
import com.chenyi.framework.manager.AsyncManager;
import com.chenyi.framework.manager.factory.AsyncFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class UserLoginService {


    @Autowired
    private UserTokenService tokenService;

    @Autowired
    private ISysUserService userService;

    /**
     * 手机号登录
     * @param phone 手机号
     * @param userType 用户类型
     * @param os 操作系统
     * @param browser 操作系统版本
     * @return 结果
     */
    public String phoneLogin(String phone,String password, String userType, String os, String browser) {
        SysUser sysUser = userService.selectUserByPhone(phone, userType);
        if (sysUser == null){
            throw new MessageException("手机号不存在");
        }
        if (!ApiUtils.matchesPassword(password,sysUser.getPassword())){
            AsyncManager.me().execute(AsyncFactory.recordAdminLogininfor(phone,browser,os,  Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
            throw new MessageException(MessageUtils.message("user.password.not.match"));
        }
        if (sysUser.getStatus().equals(UserStatus.DISABLE.getCode())){
            throw new BadRequestException(MessageUtils.message("user.blocked"));
        }
        LoginUser loginUser = new LoginUser(sysUser, os, browser);

        //清理上次Token
        if (StringUtils.isNotEmpty(loginUser.getUser().getToken()) && ChenYiConfig.getUserSsoLogin()){
            tokenService.delLoginUser(loginUser.getUser().getToken());
            //其它地方登录记录
            AsyncManager.me().execute(AsyncFactory.recordUserLogininfor(sysUser.getUserName(),browser,os, Constants.LOGIN_FAIL, MessageUtils.message("user.otherlogout"),loginUser.getUser().getToken()));
        }

        String token = tokenService.createToken(loginUser);
        //登录成功记录
        AsyncManager.me().execute(AsyncFactory.recordUserLogininfor(sysUser.getUserName(),browser,os, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"),loginUser.getToken()));

        loginUser.getUser().setToken(loginUser.getToken());
        loginUser.getUser().setLoginDate(new Date(loginUser.getLoginTime()));
        loginUser.getUser().setLoginIp(loginUser.getIpaddr());
        loginUser.getUser().setUpdateTime(DateUtils.getNowDate());
        userService.updateUser(loginUser.getUser());

        return token;
    }
}
